<?

class Radicms_modules
{
    private $modules = array();
    private $modules_installed = 0;
    
	public function __construct()
	{
		$this->reload_modules();
	}
    
    public function __get($var)
    {
        static $ci;
        isset($ci) OR $ci =& get_instance();
        return $ci->{$var};
    }
	
	public function reload_modules()
	{
		$this->load->helper('directory');	
		$modules = array();
		foreach($this->config->item('modules_locations') as $path => $location)
		{
			foreach(directory_map($path, 2) as $dir => $files)
			{
				if(is_array($files))
					if(in_array('module.php', $files))
						$modules[] = $dir;			
				
			}
		}
			
		$this->config->set_item('modules_installed', count($modules));
		
		foreach($modules as $name)
		{
		    if(in_array($name, $this->config->item('modules_enabled')))
            {	
    			//echo $name;	
    			require_once(APPPATH.'/modules/'.$name.'/module.php');		
    			$name = ucfirst($name.'_module');		
    			$module =& new $name();		
    			$this->modules[$name] = $module;
            }
		}
		
        $this->modules_installed = count($modules);   
		//echo 'modules init';
	}
	
    public function get($name)
    {
        
        return $this->modules[$name];
        
    }
    public function installed()
    {
        return $this->modules_installed;
    }
    public function get_module_uri($module = FALSE)
	{
	    if($module === FALSE)
        {
            // get uris from all modules
        }
        else 
        {
            // get uri from specific module    
        }
	}
    
    public function get_modules()
    {
        return $this->modules;
    }
}